package me.zixing.java.guiandthread.ui;

import me.zixing.java.guiandthread.domain.Rabbit;
import me.zixing.java.guiandthread.domain.SportableContent;
import me.zixing.java.guiandthread.domain.Tortoise;
import me.zixing.java.guiandthread.domain.Track;
import me.zixing.java.guiandthread.listener.MyListener;
import org.jb2011.lnf.beautyeye.ch12_progress.BEProgressBarUI;
import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by bingh on 2017/11/13.
 */
public class MainUI extends JFrame{

    private static Font font = new Font("黑体",Font.PLAIN,20);
    private static Insets insets = new Insets(0,0,0,0);
    private JButton startButton;
    private JProgressBar rabbitProgressBar;
    private JProgressBar tortoiseProgressBar;
    private ScorePanel rabbitScorePanel;
    private ScorePanel tortoiseScorePanel;
    public MainUI(){
        initUI();
        initAction();
    }

    //初始化UI
    private void initUI(){
        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBackground(Color.blue);

        JPanel topPanel = new JPanel();
        topPanel.setLayout(null);
        topPanel.setBounds(0,0,800,100);
        //topPanel.setBackground(Color.red);

        VSImagePanel topCenterPanel = new VSImagePanel();
        topCenterPanel.setLayout(null);
        topCenterPanel.setBounds(280,0,200,100);
        //topCenterPanel.setBackground(Color.cyan);

        HeadIconImagePanel rabbitHeadPanel = new HeadIconImagePanel("rabbit.png");
        rabbitHeadPanel.setBounds(5,0,100,100);
        HeadIconImagePanel tortoiseHeadPanel = new HeadIconImagePanel("tortoise.png");
        tortoiseHeadPanel.setBounds(710,0,100,100);

        rabbitScorePanel = new ScorePanel();
        rabbitScorePanel.setBounds(120,30,100,100);
        //rabbitScorePanel.setBackground(Color.blue);
        tortoiseScorePanel = new ScorePanel();
        tortoiseScorePanel.setBounds(520,30,100,100);
        //tortoiseScorePanel.setBackground(Color.cyan);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(null);
        mainPanel.setBounds(0,100,800,250);
        //mainPanel.setBackground(Color.gray);

        JLabel rabbitLab = new JLabel("卫冕兔司机");
        rabbitLab.setBounds(100,70,200,50);
        JLabel tortoiseLab = new JLabel("勇者神龟");
        tortoiseLab.setBounds(100,138,200,50);

        rabbitProgressBar = new JProgressBar();
        rabbitProgressBar.setUI(new BEProgressBarUI());
        rabbitProgressBar.setBounds(180,80,400,30);

        tortoiseProgressBar = new JProgressBar();
        //tortoiseProgressBar.setUI(new BEProgressBarUI());
        tortoiseProgressBar.setBounds(180,150,400,30);

        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(null);
        bottomPanel.setBounds(0,350,800,80);
        //bottomPanel.setBackground(Color.green);

        startButton = new JButton("开始");
        startButton.setFont(font);
        startButton.setMargin(insets);
        startButton.setSize(50,20);
        startButton.setBounds(600,15,90,40);
        startButton.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue));

        topPanel.add(topCenterPanel);
        topPanel.add(rabbitHeadPanel);
        topPanel.add(tortoiseHeadPanel);
        topPanel.add(rabbitScorePanel);
        topPanel.add(tortoiseScorePanel);

        mainPanel.add(rabbitLab);
        mainPanel.add(tortoiseLab);
        mainPanel.add(rabbitProgressBar);
        mainPanel.add(tortoiseProgressBar);

        bottomPanel.add(startButton);

        panel.add(topPanel);
        panel.add(mainPanel);
        panel.add(bottomPanel);
        this.add(panel);
    }

    //初始化Action
    private void initAction(){



        startButton.addActionListener(new MyListener());


        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Rabbit rabbit = new Rabbit("Rabbit",1,2,200,3);
                Tortoise tortoise = new Tortoise("Tortoise",3,2,200,1);
                rabbit.addListener(rabbitScorePanel);
                tortoise.addListener(tortoiseScorePanel);

                Track track = new Track(5,200);
                List<SportableContent> contents = new ArrayList<>();
                contents.add(rabbit);
                contents.add(tortoise);

                SwingWorker<Void,Void> rabbitWorker = new SwingWorker<Void,Void>() {
                    @Override public Void doInBackground() {
                        double current = 0, lengthOfTask = 100;
                        while(current<=lengthOfTask && !isCancelled()) {
                            try {
                                Thread.sleep(50);
                            } catch(Exception ie) {
                                return null;
                            }
                            setProgress((int)(100 * current / lengthOfTask));
                            current = rabbit.getX();
                        }
                        return null;
                    }
                };
                SwingWorker<Void,Void> tortoiseWorker = new SwingWorker<Void,Void>() {
                    @Override public Void doInBackground() {
                        double current = 0, lengthOfTask = 100;
                        while(current<=lengthOfTask && !isCancelled()) {
                            try {
                                Thread.sleep(50);
                            } catch(Exception ie) {
                                return null;
                            }
                            setProgress((int)(100 * current / lengthOfTask));
                            current = tortoise.getX();
                        }
                        return null;
                    }
                };
                rabbitWorker.addPropertyChangeListener(new ProgressListener(rabbitProgressBar));
                tortoiseWorker.addPropertyChangeListener(new ProgressListener(tortoiseProgressBar));
                rabbitWorker.execute();
                tortoiseWorker.execute();

                try{
                    track.start(contents);
                }catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public static void main(String[] args) throws Exception{

        try
        {
            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
            UIManager.put("RootPane.setupButtonVisible",false);
        }
        catch(Exception e) {
            e.printStackTrace();
        }

        MainUI mainUI = new MainUI();
        mainUI.setTitle("龟兔赛跑");
        mainUI.setSize(800,500);
        mainUI.setResizable(false);
        mainUI.setLocationRelativeTo(null);
        mainUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainUI.setVisible(true);
        mainUI.setFocusable(true);
        mainUI.setAlwaysOnTop(true);
    }

    class ProgressListener implements PropertyChangeListener {
        private final JProgressBar progressBar;
        ProgressListener(JProgressBar progressBar) {
            this.progressBar = progressBar;
            this.progressBar.setValue(0);
        }
        @Override public void propertyChange(PropertyChangeEvent evt) {
            String strPropertyName = evt.getPropertyName();
            if("progress".equals(strPropertyName)) {
                progressBar.setIndeterminate(false);
                int progress = (Integer)evt.getNewValue();
                progressBar.setValue(progress);
            }
        }
    }

}
